﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.Text;
using System.IO;

/// <summary>
///Request 的摘要说明
/// </summary>
public class URLRequest
{
    public URLRequest()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 以POST的方式请求
    /// </summary>
    /// <param name="url">网络路径</param>
    /// <param name="data">POST的参数</param>
    /// <returns>返回的数据流，如果出现异常，或请求失败，返回值为null</returns>
    public static Stream Post(string url, string data)
    {
        WebRequest request = HttpWebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        byte[] requestData = Encoding.GetEncoding("gb2312").GetBytes(data);
        request.ContentLength = requestData.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(requestData, 0, requestData.Length);
        requestStream.Close();
        WebResponse response = request.GetResponse();
        if (response != null)
        {
            Stream result = response.GetResponseStream();
            //response.Close();
            return result;
        }
        return null;
    }

    /// <summary>
    /// 以GET方式进行网络请求
    /// </summary>
    /// <param name="url">网络路径</param>
    /// <returns>返回的数据流，如果出现异常，或请求失败，返回值为null</returns>
    public static Stream GetRequest(string url)
    {
        WebRequest request = HttpWebRequest.Create(url);
        request.Method = "GET";
        WebResponse response = request.GetResponse();
        if (response != null)
        {
            Stream result = response.GetResponseStream();
            //response.Close();
            return result;
        }
        return null;
    }
}